package com.ruoyi.app.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.app.domain.vo.AppShareVo;
import com.ruoyi.app.domain.bo.AppShareBo;
import com.ruoyi.app.service.IAppShareService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 邀请管理控制器
 * 前端访问路由地址为:/app/share
 *
 * @author Zach
 * @date 2023-11-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/share")
public class AppShareController extends BaseController {

    private final IAppShareService iAppShareService;

    /**
     * 查询邀请管理列表
     */
    @SaCheckPermission("app:share:list")
    @GetMapping("/list")
    public TableDataInfo<AppShareVo> list(AppShareBo bo, PageQuery pageQuery) {
        return iAppShareService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邀请管理列表
     */
    @SaCheckPermission("app:share:export")
    @Log(title = "邀请管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppShareBo bo, HttpServletResponse response) {
        List<AppShareVo> list = iAppShareService.queryList(bo);
        ExcelUtil.exportExcel(list, "邀请管理", AppShareVo.class, response);
    }

    /**
     * 获取邀请管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:share:query")
    @GetMapping("/{id}")
    public R<AppShareVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iAppShareService.queryById(id));
    }

    /**
     * 新增邀请管理
     */
    @SaCheckPermission("app:share:add")
    @Log(title = "邀请管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppShareBo bo) {
        return toAjax(iAppShareService.insertByBo(bo));
    }

    /**
     * 修改邀请管理
     */
    @SaCheckPermission("app:share:edit")
    @Log(title = "邀请管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppShareBo bo) {
        return toAjax(iAppShareService.updateByBo(bo));
    }

    /**
     * 删除邀请管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("app:share:remove")
    @Log(title = "邀请管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iAppShareService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
